<?php
error_reporting(1);
function redirectCookie($url , $_c_chk = 0)
{
    if ( ! headers_sent() )
    {
        setcookie("_c_chk" , $_c_chk , 0 , "/");
        header("Location: ".$url);
    }
    else
    {
        exit("error - l");
    }
    exit();
}

if ( isset($_COOKIE['PHPSESSID'] , $_COOKIE['_c_chk']) && $_COOKIE['_c_chk'] != 'deleted' )
{
	unset($_COOKIE['PHPSESSID']);
	unset($_COOKIE['_c_chk']);
   // header("Location: op.php");
    //exit();
}
include_once("hip.php");
include_once("config.php");
include_once("function.php");
include_once("class/functions.php");
include_once("class/db.php");
include_once("class/config.php");
include_once("class/user.php");
include_once("class/template.php");
include_once("class/tmd_tpl.php");
$errmsg = "";
$user = new User();
if ( isset($_POST['login'] , $_POST['pass'] ) )  //, $_COOKIE['_c_aut']
{
    $login = htmlentities($_POST['login'] , ENT_QUOTES , 'UTF-8');
    $pass = htmlentities($_POST['pass'] , ENT_QUOTES , 'UTF-8');
    //$authinput = htmlentities($_POST['authinput'] , ENT_QUOTES , 'UTF-8');
    //if ( md5(G_KeyAuth.$authinput) == $_COOKIE['_c_aut'] )
    //{
        include_once( "class/sessions.php" );
        $conf = new Config();
        $member_limit = intval($conf->Get("member_limit"));  
        if ( $user->Login($login , $pass , $member_limit) )
        {
			//踢舊的
			//getdb("delete from k_sessions where uid='".$_SESSION['uid']."' and sid <> '".$user->sid."' ");
            $info = $user->GetCurrentUser();
            if ( $errmsg == "" )
            {
                if ( check_upid_mode($info['id']) == 1 )
                {
                    $cookData = $user->setLogin_cookie(1);
                    $_COOKIE['_c_chk'] = $cookData;
                    switch ( $info['acl'] )
                    {
                        case 1 :
                            redirectCookie("op.php?op=member&fp=confirm" , $cookData);
                            break;
                        case 2 :
                            redirectCookie("op.php?op=agent" , $cookData);
                            break;
                        case 3 :
                            redirectCookie("op.php?op=genagent" , $cookData);
                            break;
                        case 4 :
                            redirectCookie("op.php?op=shareowner" , $cookData);
                            break;
                        case 5 :
                            redirectCookie("op.php?op=subsidary" , $cookData);
                            break;
                        case 6 :
                            redirectCookie("op.php?op=finance" , $cookData);
                            break;
                        case 255 :
                            redirectCookie("op.php?op=admin" , $cookData);
                            break;
                        case 300 :
                            redirectCookie("?op=centralize" , $cookData);
                            break;
                        default :
                            $errmsg = "你没有权限！";
                            break;
                    }
                }
                else
                {
                    $errmsg = "对不起你这条管理线已被停用！";
                }
            }
        }
        else
        {
			$errmsg = "错误的帐号或密码！";
            /*$rs = getdb("select count(sid) from k_sessions where uid > 5");
            $onlinesum = $rs->f[0];
            $rs->close();

            if ( $member_limit < $onlinesum )
            {
                $errmsg = "网路连线超过负载，请稍後再试！";
            }
            else
            {
                $errmsg = "错误的帐号或密码！";
            }*/
        }
//    }
//    else
//    {
//        $errmsg = "验证码错误！";
//    }

}
$user->logout();
ob_start();
$tmd = new tmd_tpl();
if ( empty($errmsg) )
{
    $tmd->display('login/'.G_LOGINTPL.'/login_'.G_Channel);
}
else
{
    $tmd->assign('errmsg' , $errmsg);
    $tmd->display('login/error');
}
$str = ob_get_contents();
ob_end_clean();
echo $str;